/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.comm.listener.impl;

import java.util.Observable;
import java.util.Observer;

import org.kopsox.comm.message.ValueMessage;
import org.kopsox.comm.message.impl.ValueMessageImpl;
import org.kopsox.data.Value;
import org.kopsox.data.impl.Polymorph;

/**
 * This Abstract-Class can be used for Observer which should be compatible with java.util.Observer<br>
 * <b>NOTE:</b>The Value which will be sent is from the Type String!
 * 
 * @author Konrad Renner - 22.10.2010
 *
 */
public abstract class AbstractJDKObserver extends
		AbstractPropertyChangeObserver implements Observer {

	private static final long serialVersionUID = 6439206350628123006L;

	/* (non-Javadoc)
	 * @see org.kopsox.comm.listener.impl.AbstractObserver#notification(org.kopsox.comm.message.ValueMessage)
	 */
	@Override
	public abstract void notification(ValueMessage message);

	/* (non-Javadoc)
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
	 */
	@Override
	public void update(Observable observable, Object newValue) {
		
		String newValueStr;
		if(newValue == null) {
			newValueStr = null;
		}else if(newValue instanceof java.sql.Timestamp || newValue instanceof java.sql.Time) {
			newValueStr = newValue.toString();
		}else if(newValue instanceof java.util.Date) {
			newValueStr = DATE_FORMAT.format((java.util.Date)newValue);
		}else {
			newValueStr = newValue.toString();
		}

		// Message creation
		Value<String> value = new Polymorph<String>(newValueStr);
		ValueMessageImpl message = ValueMessageImpl.createValueMessage(value);
		message.setMessageSender(observable);

		// Sending the message
		notification(message);
	}

}
